/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GenerateDSAKeysPanel.java
 *
 * Created on 01.02.2009, 14:56:59
 */
package jopensslgui;

import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 *
 * @author uli
 */
public class GenerateDSAKeysPanel extends javax.swing.JPanel
{

    /** Creates new form GenerateDSAKeysPanel */
    public GenerateDSAKeysPanel()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dsaParamPanel = new javax.swing.JPanel();
        selectParamOutputFileButton = new javax.swing.JButton();
        paramOutputFileField = new javax.swing.JTextField();
        paramOutputFileLabel = new javax.swing.JLabel();
        sizeComboBox = new javax.swing.JComboBox();
        sizeLabel = new javax.swing.JLabel();
        generateParamButton = new javax.swing.JButton();
        directGenerationCheckbox = new javax.swing.JCheckBox();
        dsaKeyPanel = new javax.swing.JPanel();
        paramInputFileLabel = new javax.swing.JLabel();
        selectParamInputFileButton = new javax.swing.JButton();
        paramInputFileField = new javax.swing.JTextField();
        selectKeyOutputFileButton = new javax.swing.JButton();
        keyOutputFileLabel = new javax.swing.JLabel();
        keyOutputFileField = new javax.swing.JTextField();
        generateKeyButton = new javax.swing.JButton();

        dsaParamPanel.setBorder(javax.swing.BorderFactory.createTitledBorder( i18n.getString("GenerateDSAKeysPanel.dsaParamPanel.border.title"))); // NOI18N

        selectParamOutputFileButton.setText(i18n.getString("GenerateDSAKeysPanel.selectParamOutputFileButton.text")); // NOI18N
        selectParamOutputFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectParamOutputFileButtonselectRandomOutputFileButtonActionPerformed(evt);
            }
        });

        paramOutputFileLabel.setText( i18n.getString("GenerateDSAKeysPanel.paramOutputFileLabel.text")); // NOI18N

        sizeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "128", "256", "512", "1024" }));
        sizeComboBox.setSelectedIndex(3);

        sizeLabel.setText( i18n.getString("GenerateDSAKeysPanel.sizeLabel.text")); // NOI18N

        generateParamButton.setText( i18n.getString("GenerateDSAKeysPanel.generateParamButton.text")); // NOI18N
        generateParamButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateParamButtonActionPerformed(evt);
            }
        });

        directGenerationCheckbox.setText( i18n.getString("GenerateDSAKeysPanel.directGenerationCheckbox.text")); // NOI18N
        directGenerationCheckbox.setToolTipText( i18n.getString("GenerateDSAKeysPanel.directGenerationCheckbox.toolTipText")); // NOI18N

        javax.swing.GroupLayout dsaParamPanelLayout = new javax.swing.GroupLayout(dsaParamPanel);
        dsaParamPanel.setLayout(dsaParamPanelLayout);
        dsaParamPanelLayout.setHorizontalGroup(
            dsaParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, dsaParamPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(dsaParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(generateParamButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
                    .addGroup(dsaParamPanelLayout.createSequentialGroup()
                        .addGroup(dsaParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(dsaParamPanelLayout.createSequentialGroup()
                                .addComponent(paramOutputFileLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(dsaParamPanelLayout.createSequentialGroup()
                                .addComponent(sizeLabel)
                                .addGap(55, 55, 55)))
                        .addGroup(dsaParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(sizeComboBox, 0, 102, Short.MAX_VALUE)
                            .addComponent(paramOutputFileField))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(dsaParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(directGenerationCheckbox, 0, 0, Short.MAX_VALUE)
                            .addComponent(selectParamOutputFileButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(36, 36, 36))
        );
        dsaParamPanelLayout.setVerticalGroup(
            dsaParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dsaParamPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(dsaParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sizeLabel)
                    .addComponent(sizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(directGenerationCheckbox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(dsaParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(paramOutputFileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(paramOutputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectParamOutputFileButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(generateParamButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        dsaKeyPanel.setBorder(javax.swing.BorderFactory.createTitledBorder( i18n.getString("GenerateDSAKeysPanel.dsaKeyPanel.border.title"))); // NOI18N

        paramInputFileLabel.setText( i18n.getString("GenerateDSAKeysPanel.paramInputFileLabel.text")); // NOI18N

        selectParamInputFileButton.setText(i18n.getString("GenerateDSAKeysPanel.selectParamInputFileButton.text")); // NOI18N
        selectParamInputFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectParamInputFileButtonActionPerformed(evt);
            }
        });

        selectKeyOutputFileButton.setText(i18n.getString("GenerateDSAKeysPanel.selectKeyOutputFileButton.text")); // NOI18N
        selectKeyOutputFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectKeyOutputFileButtonselectRandomOutputFileButtonActionPerformed(evt);
            }
        });

        keyOutputFileLabel.setText( i18n.getString("GenerateDSAKeysPanel.keyOutputFileLabel.text")); // NOI18N

        generateKeyButton.setText( i18n.getString("GenerateDSAKeysPanel.generateKeyButton.text")); // NOI18N
        generateKeyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateKeyButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout dsaKeyPanelLayout = new javax.swing.GroupLayout(dsaKeyPanel);
        dsaKeyPanel.setLayout(dsaKeyPanelLayout);
        dsaKeyPanelLayout.setHorizontalGroup(
            dsaKeyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, dsaKeyPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(dsaKeyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(generateKeyButton, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                    .addGroup(dsaKeyPanelLayout.createSequentialGroup()
                        .addGroup(dsaKeyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(paramInputFileLabel)
                            .addComponent(keyOutputFileLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(dsaKeyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(paramInputFileField, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                            .addComponent(keyOutputFileField, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(dsaKeyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(selectParamInputFileButton)
                            .addComponent(selectKeyOutputFileButton))))
                .addGap(42, 42, 42))
        );
        dsaKeyPanelLayout.setVerticalGroup(
            dsaKeyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dsaKeyPanelLayout.createSequentialGroup()
                .addGroup(dsaKeyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(paramInputFileLabel)
                    .addComponent(paramInputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectParamInputFileButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(dsaKeyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(keyOutputFileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(keyOutputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectKeyOutputFileButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(generateKeyButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dsaKeyPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(dsaParamPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dsaParamPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dsaKeyPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void selectParamOutputFileButtonselectRandomOutputFileButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectParamOutputFileButtonselectRandomOutputFileButtonActionPerformed
    {//GEN-HEADEREND:event_selectParamOutputFileButtonselectRandomOutputFileButtonActionPerformed
        fileChooser.setSelectedFile(new File(paramOutputFileField.getText()));
        fileChooser.showSaveDialog(this);
        paramOutputFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}//GEN-LAST:event_selectParamOutputFileButtonselectRandomOutputFileButtonActionPerformed

    private void generateParamButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_generateParamButtonActionPerformed
    {//GEN-HEADEREND:event_generateParamButtonActionPerformed
        try
        {
            StringBuilder cmdBuilder = new StringBuilder("openssl dsaparam -out ");
            cmdBuilder.append(paramOutputFileField.getText());
            cmdBuilder.append(" " + (String) sizeComboBox.getSelectedItem());
            if (directGenerationCheckbox.isSelected())
            {
                /**
                 * Make OpenSSL to generate the key directly in one step
                 */
                cmdBuilder.append(" -genkey");
            }
            Process p = Runtime.getRuntime().exec(cmdBuilder.toString());
            p.waitFor();
        }
        catch (InterruptedException ex)
        {
            logger.log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
            logger.log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_generateParamButtonActionPerformed

    private void selectParamInputFileButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectParamInputFileButtonActionPerformed
    {//GEN-HEADEREND:event_selectParamInputFileButtonActionPerformed
        fileChooser.setSelectedFile(new File(paramInputFileField.getText()));
        fileChooser.showSaveDialog(this);
        paramInputFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}//GEN-LAST:event_selectParamInputFileButtonActionPerformed

    private void selectKeyOutputFileButtonselectRandomOutputFileButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectKeyOutputFileButtonselectRandomOutputFileButtonActionPerformed
    {//GEN-HEADEREND:event_selectKeyOutputFileButtonselectRandomOutputFileButtonActionPerformed
        fileChooser.setSelectedFile(new File(keyOutputFileField.getText()));
        fileChooser.showSaveDialog(this);
        keyOutputFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}//GEN-LAST:event_selectKeyOutputFileButtonselectRandomOutputFileButtonActionPerformed

    private void generateKeyButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_generateKeyButtonActionPerformed
    {//GEN-HEADEREND:event_generateKeyButtonActionPerformed
        try
        {
            StringBuilder cmdBuilder = new StringBuilder("openssl gendsa -out ");
            cmdBuilder.append(keyOutputFileField.getText());
            cmdBuilder.append(" " + paramInputFileField.getText());
            logger.fine("Executing \'" + cmdBuilder.toString() + "\'");
            Process p = Runtime.getRuntime().exec(cmdBuilder.toString());
            p.waitFor();

            int exitCode = p.exitValue();
            if (exitCode != 0)
            {
                logger.log(Level.SEVERE, "OpenSSL returned exit status " + exitCode);
            }
        }
        catch (InterruptedException ex)
        {
            logger.log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
            logger.log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_generateKeyButtonActionPerformed
    private JFileChooser fileChooser = new JFileChooser();
    private ResourceBundle i18n = ResourceBundle.getBundle("jopensslgui/Bundle"); //NOI18N
    private Logger logger = JOpenSSLFrame.logger;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox directGenerationCheckbox;
    private javax.swing.JPanel dsaKeyPanel;
    private javax.swing.JPanel dsaParamPanel;
    private javax.swing.JButton generateKeyButton;
    private javax.swing.JButton generateParamButton;
    private javax.swing.JTextField keyOutputFileField;
    private javax.swing.JLabel keyOutputFileLabel;
    private javax.swing.JTextField paramInputFileField;
    private javax.swing.JLabel paramInputFileLabel;
    private javax.swing.JTextField paramOutputFileField;
    private javax.swing.JLabel paramOutputFileLabel;
    private javax.swing.JButton selectKeyOutputFileButton;
    private javax.swing.JButton selectParamInputFileButton;
    private javax.swing.JButton selectParamOutputFileButton;
    private javax.swing.JComboBox sizeComboBox;
    private javax.swing.JLabel sizeLabel;
    // End of variables declaration//GEN-END:variables
}
